package com.coffeeloversupportlibrary.coffeelover.mercury.my.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;

import androidx.cardview.widget.CardView;

import com.bumptech.glide.Glide;
import com.coffeeloversupportlibrary.R;
import com.coffeeloversupportlibrary.coffeelover.mercury.entity.WantOrDrinked;


import java.util.ArrayList;
import java.util.List;

/**
 * @author 盛守义
 * @createTime 2021/11/26 14:16
 * @projectName TurningCoffee
 * @className WantOrDrinkedAdapter.java
 * @description TODO
 */
public class WantOrDrinkedAdapter extends BaseAdapter {
    private List<WantOrDrinked.DataDTO.ListDTO> list = new ArrayList<>();
    private Context context;

    private ImageView iv_coffee_category;
    private TextView tv_name, tv_score, tv_brief;
    private RatingBar rb_raise;
    private CardView card;
    private TextView[] tag;

    private CardOnClickListerner mListener;

    public WantOrDrinkedAdapter(){}

    public WantOrDrinkedAdapter(Context context, List<WantOrDrinked.DataDTO.ListDTO> coffees, CardOnClickListerner mListener){
        this.context = context;
        this.list = coffees;
        this.mListener = mListener;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int i) {
        return list.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        LayoutInflater layoutInflater = LayoutInflater.from(context);
        View layout = layoutInflater.inflate(R.layout.category_item, null);

        iv_coffee_category = layout.findViewById(R.id.iv_categroy_coffee);
        tv_brief = layout.findViewById(R.id.tv_category_brief);
        tv_name = layout.findViewById(R.id.tv_categoyr_coffeename);
        tv_score = layout.findViewById(R.id.tv_category_score);
        rb_raise = layout.findViewById(R.id.rb_appraise);

        Glide.with(context).load(list.get(i).getCoffeeImg()).into(iv_coffee_category);
        tv_name.setText(list.get(i).getCoffeeName());
        tv_score.setText(list.get(i).getCoffeeStar()+" 分");
        rb_raise.setRating(list.get(i).getCoffeeStar());
        if(list.get(i).getCoffeeBrief()==null){
            tv_brief.setText("");
        }else{
            tv_brief.setText(list.get(i).getCoffeeBrief());
        }

        tag = new TextView[3];
        tag[0] = layout.findViewById(R.id.tv_tag1);
        tag[1] = layout.findViewById(R.id.tv_tag2);
        tag[2] = layout.findViewById(R.id.tv_tag3);
        String[] types = list.get(i).getCoffeeTypes().split(",");

        for(int n = 0; n < 3; n++){
            tag[n].setText("#"+types[n]);
        }

        card = layout.findViewById(R.id.dv_card_cate);
        card.setTag(i);
        card.setOnClickListener(mListener);

        return layout;
    }

    public abstract static class CardOnClickListerner implements View.OnClickListener{
        /**
         * 基类的onClick方法
         */
        @Override
        public void onClick(View v) {
            myOnClick((Integer) v.getTag(), v);
        }
        public abstract void myOnClick(int position, View v);
    }
}
